package com.team.order.controller;

import com.team.common.core.web.domain.AjaxResult;
import com.team.order.utils.PaymentUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 模拟支付控制器
 * 用于测试支付流程，模拟第三方支付平台
 * @author 梁国瑞
 */
@Slf4j
@RestController
@RequestMapping("/mock/payment")
@RequiredArgsConstructor
public class MockPaymentController {
    
    private final PaymentUtil paymentUtil;
    
    /**
     * 模拟支付成功
     */
    @PostMapping("/success/{orderNo}")
    public AjaxResult simulatePaymentSuccess(@PathVariable String orderNo) {
        try {
            boolean success = paymentUtil.simulatePaymentSuccess(orderNo);
            if (success) {
                log.info("模拟支付成功，订单号：{}", orderNo);
                return AjaxResult.success("支付成功");
            } else {
                return AjaxResult.error("支付失败");
            }
        } catch (Exception e) {
            log.error("模拟支付成功失败，订单号：{}", orderNo, e);
            return AjaxResult.error(e.getMessage());
        }
    }
    
    /**
     * 模拟支付失败
     */
    @PostMapping("/fail/{orderNo}")
    public AjaxResult simulatePaymentFail(@PathVariable String orderNo) {
        try {
            boolean success = paymentUtil.simulatePaymentFail(orderNo);
            if (success) {
                log.info("模拟支付失败，订单号：{}", orderNo);
                return AjaxResult.success("支付失败");
            } else {
                return AjaxResult.error("操作失败");
            }
        } catch (Exception e) {
            log.error("模拟支付失败操作异常，订单号：{}", orderNo, e);
            return AjaxResult.error(e.getMessage());
        }
    }
    
    /**
     * 查询支付状态
     */
    @GetMapping("/status/{orderNo}")
    public AjaxResult queryPaymentStatus(@PathVariable String orderNo) {
        try {
            String status = paymentUtil.queryPaymentStatus(orderNo);
            log.info("查询支付状态，订单号：{}，状态：{}", orderNo, status);
            return AjaxResult.success(status);
        } catch (Exception e) {
            log.error("查询支付状态失败，订单号：{}", orderNo, e);
            return AjaxResult.error(e.getMessage());
        }
    }
    
    /**
     * 模拟退款
     */
    @PostMapping("/refund")
    public AjaxResult refund(@RequestParam String orderNo, @RequestParam BigDecimal amount) {
        try {
            boolean success = paymentUtil.refund(orderNo, amount);
            if (success) {
                log.info("模拟退款成功，订单号：{}，退款金额：{}", orderNo, amount);
                return AjaxResult.success("退款成功");
            } else {
                return AjaxResult.error("退款失败");
            }
        } catch (Exception e) {
            log.error("模拟退款失败，订单号：{}", orderNo, e);
            return AjaxResult.error(e.getMessage());
        }
    }
    
    /**
     * 模拟支付宝支付页面
     */
    @GetMapping("/alipay/pay")
    public String alipayPayPage(@RequestParam String orderNo) {
        log.info("访问模拟支付宝支付页面，订单号：{}", orderNo);
        
        return "<!DOCTYPE html>\n" +
            "<html>\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "    <title>模拟支付宝支付</title>\n" +
            "    <style>\n" +
            "        body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }\n" +
            "        .container { max-width: 500px; margin: 0 auto; }\n" +
            "        .payment-info { background: #f5f5f5; padding: 20px; border-radius: 10px; margin: 20px 0; }\n" +
            "        .btn { padding: 10px 20px; margin: 10px; border: none; border-radius: 5px; cursor: pointer; }\n" +
            "        .btn-success { background: #28a745; color: white; }\n" +
            "        .btn-danger { background: #dc3545; color: white; }\n" +
            "    </style>\n" +
            "</head>\n" +
            "<body>\n" +
            "    <div class=\"container\">\n" +
            "        <h2>模拟支付宝支付</h2>\n" +
            "        <div class=\"payment-info\">\n" +
            "            <p><strong>订单号：</strong>" + orderNo + "</p>\n" +
            "            <p><strong>支付金额：</strong>¥100.00</p>\n" +
            "            <p><strong>商品描述：</strong>汽车维修服务</p>\n" +
            "        </div>\n" +
            "        <div>\n" +
            "            <button class=\"btn btn-success\" onclick=\"paySuccess()\">支付成功</button>\n" +
            "            <button class=\"btn btn-danger\" onclick=\"payFail()\">支付失败</button>\n" +
            "        </div>\n" +
            "    </div>\n" +
            "    \n" +
            "    <script>\n" +
            "        function paySuccess() {\n" +
            "            fetch('/mock/payment/success/" + orderNo + "', { method: 'POST' })\n" +
            "                .then(response => response.json())\n" +
            "                .then(data => {\n" +
            "                    alert('支付成功！');\n" +
            "                    window.close();\n" +
            "                })\n" +
            "                .catch(error => {\n" +
            "                    alert('操作失败：' + error);\n" +
            "                });\n" +
            "        }\n" +
            "        \n" +
            "        function payFail() {\n" +
            "            fetch('/mock/payment/fail/" + orderNo + "', { method: 'POST' })\n" +
            "                .then(response => response.json())\n" +
            "                .then(data => {\n" +
            "                    alert('支付失败！');\n" +
            "                    window.close();\n" +
            "                })\n" +
            "                .catch(error => {\n" +
            "                    alert('操作失败：' + error);\n" +
            "                });\n" +
            "        }\n" +
            "    </script>\n" +
            "</body>\n" +
            "</html>";
    }
    
    /**
     * 清除所有模拟数据
     */
    @PostMapping("/clear")
    public AjaxResult clearAllData() {
        try {
            paymentUtil.clearAllData();
            log.info("清除所有模拟支付数据");
            return AjaxResult.success("数据清除成功");
        } catch (Exception e) {
            log.error("清除模拟支付数据失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }
} 